package easier.app.x.file;

import android.content.Context;
import android.os.Build.VERSION_CODES;
import android.os.Environment;
import android.os.storage.StorageManager;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.core.content.ContextCompat;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

public class AppFileManager {

    /**
     * @param context 内部存储,缓存目录,容量有限,随app删除,空间不够时系统自动删除,先删除最晚修改的
     * @param name    缓存文件名字
     *
     * @return 缓存目录
     */
    public static File cacheFile( Context context, @NonNull String name ) {

        return new File( context.getCacheDir(), name );
    }

    /**
     * @param context 内部存储,文件目录,容量有限,随app删除
     * @param name    文件名字
     *
     * @return 文件
     */
    public static File file( Context context, @NonNull String name ) {

        return new File( context.getFilesDir(), name );
    }

    public static File externalCacheFile( Context context, @NonNull String name ) {

        return new File( context.getExternalCacheDir(), name );
    }

    @Nullable
    public static File externalFilesFile( Context context, String type, @NonNull String name ) {

        if ( isExternalStorageWritable() ) {
            return new File( context.getExternalFilesDir( type ), name );
        } else {
            return null;
        }
    }

    @Nullable
    public static File externalFilesFile( Context context, @NonNull String name ) {

        return externalFilesFile( context, null, name );
    }

    // Checks if a volume containing external storage is available
    // for read and write.
    public static boolean isExternalStorageWritable() {

        return Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED );
    }

    @RequiresApi( api = VERSION_CODES.O )
    public static void allocateFileBytes( Context context, long need ) throws IOException {

        allocateBytes( context, need, context.getFilesDir() );
    }

    @RequiresApi( api = VERSION_CODES.O )
    public static void allocateExternalFileBytes( Context context, long need ) throws IOException {

        allocateBytes( context, need, context.getExternalFilesDir( null ) );
    }

    @RequiresApi( api = VERSION_CODES.O )
    private static void allocateBytes( Context context, long need, File file ) throws IOException {

        StorageManager storageManager = ContextCompat.getSystemService( context, StorageManager.class );
        if ( storageManager != null ) {
            UUID appSpecificInternalDirUuid = storageManager.getUuidForPath( file );
            long availableBytes = storageManager.getAllocatableBytes( appSpecificInternalDirUuid );
            if ( availableBytes >= need ) {
                storageManager.allocateBytes( appSpecificInternalDirUuid, need );
            }
        }
        /*else {
            // To request that the user remove all app cache files instead, set
            // "action" to ACTION_CLEAR_APP_CACHE.
            Intent storageIntent = new Intent();
            storageIntent.setAction( ACTION_MANAGE_STORAGE );
        }*/
    }
}
